# -*- coding: utf-8 -*-

import os
import copy
from xlsxtpl.writerx import BookWriter
import pandas as pd


def write_test():
    pth = os.path.dirname(__file__)
    fname = os.path.join(pth, 'example3.xlsx')
    writer = BookWriter(fname)
    writer.jinja_env.globals.update(dir=dir, getattr=getattr)

    df1=pd.read_excel('1-9.xlsx',sheet_name='总体')
    df2=pd.read_excel('1-9.xlsx',sheet_name='企业')
    location= ['安国','大屯','河口','胡寨','敬安','开发区','龙固','鹿楼','沛城','栖山','魏庙','五段','杨屯','张寨','张庄','朱寨']
    payloads=[]
    for i in location:
        df_l1=df1[df1['区域']==i]
        lo_info={'location':df_l1.iloc[0,0],'last_month':int(df_l1.iloc[0,1]),'this_month':int(df_l1.iloc[0,2])}
        rows=df2[df2['区域']==i].values.tolist()
        lo_info['rows']=rows
        lo_info['sheet_name'] = i
        payloads.append(lo_info)
    # person_info2['rows'] = rows
    # person_info2['tpl_name'] = 'en'
    # person_info3 = copy.copy(person_info2)
    # person_info3['sheet_name'] = 'hello sheet'
    writer.render_book(payloads=payloads)
    fname = os.path.join(pth, 'result00.xlsx')
    writer.save(fname)
    # payloads = [person_info3, person_info, person_info2]
    # writer.render_book(payloads=payloads)
    # writer.render_sheet(person_info2, 'form2', 1)
    # fname = os.path.join(pth, 'result01.xlsx')
    # writer.save(fname)


if __name__ == "__main__":
    write_test()
